//
//  XYEmoKeyboard.swift
//  SinaWB_XY
//
//  Created by 小熊猫 on 15/10/22.
//  Copyright © 2015年 小熊猫. All rights reserved.
//

import UIKit

private let CELL_ID = "cell_Identifier"

class XYEmoKeyboard: UIView,UICollectionViewDataSource,UICollectionViewDelegate,XYKeybordToolBarDeletage{

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
    
        
        addSubview(keybordToolBar)
        
        addSubview(emoticonKeybord)
        
        addSubview(pageView)

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //keybordtoolbar的代理方法
    func didKeybordToolBarButtonWithPage(toolbar: XYKeybordToolBar, type: XYKeybordToolBarType) {
        
        let pageNum = XYEmotionTool.emotionWithButtonType(type)
        //创建坐标.   第一个参数是第几行. 后面是第几组.
        let indexPath = NSIndexPath(forRow: pageNum, inSection: 0)
        
        emoticonKeybord.scrollToItemAtIndexPath(indexPath, atScrollPosition:UICollectionViewScrollPosition.None, animated: false)
    }
    

    //创建键盘下面的按钮,
    private lazy var keybordToolBar:XYKeybordToolBar = {
        let toolBar = XYKeybordToolBar()
        toolBar.delegate = self
        return toolBar
        
    }()
    
    //创建表情键盘
    private lazy var emoticonKeybord:UICollectionView = {
        //设置属性,格式
        let layout = UICollectionViewFlowLayout()
        
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        layout.minimumLineSpacing = 0
        
        let collection = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
        collection.backgroundColor = UIColor.clearColor()
        
        collection.dataSource = self
        collection.delegate = self
        
        collection.pagingEnabled = true
        collection.showsHorizontalScrollIndicator = false
        
        //注册cell
        collection.registerClass(XYEmoCollectionCell.self, forCellWithReuseIdentifier: CELL_ID)
        
        return collection
    }()
    
    //加载分页
    private lazy var pageView:UIPageControl = {
        
        let pageView = UIPageControl()
        
        pageView.currentPageIndicatorTintColor = UIColor.orangeColor()
        pageView.pageIndicatorTintColor = UIColor.greenColor()
        
        pageView.currentPage = 1
//        pageView.numberOfPages = 5
        
        return pageView
        }()
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //设置toolbar的位置
        keybordToolBar.width = width
        keybordToolBar.height = 36
        keybordToolBar.y = self.height - keybordToolBar.height
        //设置表情键盘的位置
        emoticonKeybord.size = CGSizeMake(self.width, keybordToolBar.y)
        //设置cell 的大小
        let layout = emoticonKeybord.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = emoticonKeybord.size
        
        pageView.centerX = self.width * 0.5
        pageView.y = self.height - keybordToolBar.height  - 10

    }
}





//实现表情键盘的代理和数据源方法
extension XYEmoKeyboard{
    //数据源
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return XYEmotionTool.allEmotionNum()
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CELL_ID, forIndexPath: indexPath) as! XYEmoCollectionCell
        
        
        //给cell的表情模型赋值
        cell.emotionModel =  XYEmotionTool.emotionCellPageModelWithPageNum(indexPath.row)

        
        
        return cell
    }
    
    
    //监测键盘滚动到什么位置
    func scrollViewDidScroll(scrollView: UIScrollView) {
        //算出当前滚动到那一页
        let pageNum = Int(scrollView.contentOffset.x / scrollView.width + 0.5)
        //根据页数.求出所在的表情类型
        let type = XYEmotionTool.emotionWithPageNum(pageNum)
        
        keybordToolBar.seleButtonWithType(type)
        
        //根据类型.计算出这个类型有多少ye
        self.pageView.numberOfPages = XYEmotionTool.emotionlengthWithButtonType(type)
        
        //根据现在滚动的页数.计算在这个类型的哪一页
        self.pageView.currentPage = XYEmotionTool.emotionSeleNumWithPageNum(pageNum)
        
        
        
    }
    
}







